// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import UIKit

/**
 This structure holds the display parameters for the overlay to be drawon on a detected object.
 */
struct ObjectOverlay {
    let name: String
    let borderRect: CGRect
    let nameStringSize: CGSize
    let imageSize:CGSize
    let color: UIColor
    let font: UIFont
    var pointResults: [Int] = []
    var timeResults: [Float32] = []
}

/**
 This UIView draws overlay on a detected object.
 */
class OverlayView: UIView,iPhoneModelS{
    
    var tempResults: [Int] = []
    
    
    var objectOverlays: [ObjectOverlay] = []
    private let cornerRadius: CGFloat = 10.0
    private let stringBgAlpha: CGFloat
        = 0.7
    private let lineWidth: CGFloat = 3
    private let stringFontColor = UIColor.white
    private let stringHorizontalSpacing: CGFloat = 13.0
    private let stringVerticalSpacing: CGFloat = 7.0
    
    override func draw(_ rect: CGRect) {
        
        // Drawing code
        for objectOverlay in objectOverlays {
            
            drawBorders(of: objectOverlay)
            drawPoints(of: objectOverlay)
            //            drawBackground(of: objectOverlay)
            //            drawName(of: objectOverlay)
        }
    }
    
    /**
     This method draws the borders of the detected objects.
     */
    func drawBorders(of objectOverlay: ObjectOverlay) {
        
        let path = UIBezierPath(rect: objectOverlay.borderRect)
        path.lineWidth = lineWidth
        objectOverlay.color.setStroke()
        
        path.stroke()
    }
    
    func drawPoints(of objectOverlay: ObjectOverlay) {
        tempResults = objectOverlay.pointResults

        var scale:CGAffineTransform
        if iPhone() == .iPhoneX {
            scale = CGAffineTransform(scaleX:kWindowW/objectOverlay.imageSize.width , y: 734/objectOverlay.imageSize.height)
        } else {
            scale = CGAffineTransform(scaleX:kWindowW/objectOverlay.imageSize.width , y: kWindowH/objectOverlay.imageSize.height )
        }
        
        for i in stride(from: 0, to: objectOverlay.pointResults.count, by: 2) {
            //
            var tempX = objectOverlay.imageSize.width-CGFloat(tempResults[i])
            var tempY = CGFloat(tempResults[i+1])
            let  tempRect =  CGRect(x: tempX, y: tempY, width: 1, height: 1)
            
            let convertedRect = tempRect.applying(scale)
            
            tempX = convertedRect.origin.x
            tempY = convertedRect.origin.y
            
            let color = UIColor.yellow
            color.set() // 设置线条颜色
            // 画点
            let aPath = UIBezierPath(arcCenter: CGPoint(x: tempX,y: tempY), radius: 1, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
            aPath.lineWidth = 4.0 // 线条宽度
            aPath.stroke() // Draws line 根据坐标点连线，不填充
            aPath.fill() // Draws line 根据坐标点连线，填充
 
        }
        
    }
    
    /**
     This method draws the background of the string.
     */
    func drawBackground(of objectOverlay: ObjectOverlay) {
        
        let stringBgRect = CGRect(x: objectOverlay.borderRect.origin.x, y: objectOverlay.borderRect.origin.y , width: 0 * stringHorizontalSpacing + objectOverlay.nameStringSize.width, height: 0 * stringVerticalSpacing + objectOverlay.nameStringSize.height
        )
    
        let stringBgPath = UIBezierPath(rect: stringBgRect)
        objectOverlay.color.withAlphaComponent(stringBgAlpha).setFill()
        stringBgPath.fill()
    }
    
    /**
     This method draws the name of object overlay.
     //     */
    //    func drawName(of objectOverlay: ObjectOverlay) {
    //
    //        // Draws the string.
    //        let stringRect = CGRect(x: objectOverlay.borderRect.origin.x + stringHorizontalSpacing, y: objectOverlay.borderRect.origin.y + stringVerticalSpacing, width: objectOverlay.nameStringSize.width, height: objectOverlay.nameStringSize.height)
    //
    //        let attributedString = NSAttributedString(string: objectOverlay.name, attributes: [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue) : stringFontColor, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue) : objectOverlay.font])
    //        attributedString.draw(in: stringRect)
    //    }
    
}

